# Shadowing Political Elites replication script 

library(tidyverse)
library(stm)
library(wordcloud)
library(igraph)
library(ggplot2)


data <- read_csv("pradhantextall2.csv")
processed <- textProcessor(data$text, metadata = data)
out <- prepDocuments(processed$documents, processed$vocab, processed$meta)
docs <- out$text
vocab <- out$vocab
meta <-out$meta
set.seed(02138)
PradPrevFit <- stm(documents = out$documents, vocab = out$vocab, K = 20, prevalence =~ subject + sday + weekend + morning, max.em.its = 200, data = out$meta, init.type = "Spectral")

# Summarize topics for input into Figure 4 (this code provides the information extracted for the figure)
pdf('./figures/Figure4.pdf')
par(mfrow = c(1,2), fin = c(6,4), adj = 0.4)
plot(PradPrevFit, type = 'labels', topics = c(2, 14:16), n = 9, labeltype = 'prob', main = 'Figure 4')
dev.off()

# Generate Word Cloud for Figure 5
pdf('./figures/Figure5.pdf')
set.seed(02139)
cloud(PradPrevFit, topic = 19, scale = c(2,.60))
dev.off()